

/**
 * 解析高德路径规划返回的数组
 * @param {*} path  高德导航路径数组 
 */
import CoordTransform from '../../transform/CoordTransform'

function parseGCJPolyline(path){
  let polylines = []
  path.steps.forEach(item => {
    let positions = item.polyline.split(';')
    positions.forEach(line => {
      let step = line.split(',')
      let polyline = CoordTransform.GCJ02ToWGS84(step[0]  ,step[1])
      polylines.push(polyline[0])
      polylines.push(polyline[1])
    })
  })
  return polylines
}

import axios from 'axios'
/**
 * 通过起始点 结束点 请求高德路径
 * @param {*} startCoord WGS84 经纬度坐标
 * @param {*} endCoord WGS84 经纬度坐标
 * @param {*} callback 请求结果回调
 * @param {*} option 请求高德路径规划的参数
 * @returns GCJ-20 高德路径坐标
 */

function getGCJPolyline(startCoord , endCoord , callback, option = {}){

  startCoord = CoordTransform.WGS84ToGCJ02(startCoord[0] , startCoord[1])
  endCoord =CoordTransform.WGS84ToGCJ02(endCoord[0] , endCoord[1])

  axios.get('https://restapi.amap.com/v3/direction/driving' , {
    params : {
      "output" : "json",
      "key" : "87b2d9a40dcf3f342b994e8ccb8b5a87",
      "origin" : startCoord[0] + "," + startCoord[1],
      "destination" : endCoord[0] + "," + endCoord[1],
      "strategy" : 10,
      "extensions" : "all",
      ...option
    }
  }).then(({data}) => {
    // console.log(data)
    if(data && data.route.paths){
      callback && callback(data.route.paths)
      return
    }
    callback && callback(null)
  }).catch(err => {
    callback && callback(null)
  })
}

export {
  parseGCJPolyline,
  getGCJPolyline
} 